package com.yenlien.traceyun.control.controller;

import com.alibaba.fastjson.JSONObject;
import com.yenlien.traceyun.control.dao.ModelClassifyDao;
import com.yenlien.traceyun.control.entity.ModelClassify;
import com.yenlien.traceyun.control.service.ModelClassifyService;
import com.yenlien.traceyun.webase.controller.BaseController;
import com.yenlien.traceyun.webase.exception.BusinessException;
import com.yenlien.traceyun.webase.utils.CheckParams;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by LiuSha on 2020/3/24.
 * 模型库分类
 */
@RestController
@RequestMapping(value = "/model/classify")
public class ModelClassifyController extends BaseController {
    @Autowired
    private ModelClassifyService modelClassifyService;
    @Autowired
    private ModelClassifyDao modelClassifyDao;

    @RequestMapping(value = "/findByList", method = RequestMethod.POST)
    public Object findByList(@RequestBody ModelClassify modelClassify) {
        List<ModelClassify> page = modelClassifyService.findByList(modelClassify);
        if (page.size() > 0) {
            for (ModelClassify modelClassify1 : page) {
                modelClassify1.setModelClassifyList(modelClassifyDao.findByParentId(modelClassify1.getId()));
            }
        }
        return page;
    }

    //一级分类
    @RequestMapping(value = "/findByOne", method = RequestMethod.POST)
    public Object findByOne(@RequestBody JSONObject param) {
        CheckParams.checkJson(param, new String[]{"userType", "companyId"});
//        return success(modelClassifyService.findByOne(param));
        return success(modelClassifyService.findByCompany(param.getString("companyId")));
    }

    //下级分类
    @RequestMapping(value = "/findByParentId/{parentId}", method = RequestMethod.GET)
    public Object findByParentId(@PathVariable String parentId) throws BusinessException {
        return success(modelClassifyService.findByParentId(parentId));
    }

    @RequestMapping(value = "/save", method = RequestMethod.POST)
    public Object save(ModelClassify modelClassify, HttpServletRequest request) throws Exception {
        List<MultipartFile> files = new ArrayList<>();
        if (!StringUtils.isEmpty(((MultipartHttpServletRequest) request).getFile("files"))) {
            files = ((MultipartHttpServletRequest) request).getFiles("files");
        }
        modelClassifyService.save(modelClassify, files);
        return success("success");
    }

    @RequestMapping(value = "/update", method = RequestMethod.POST)
    public Object update(ModelClassify modelClassify, HttpServletRequest request) throws Exception {
        List<MultipartFile> files = new ArrayList<>();
        if (!StringUtils.isEmpty(((MultipartHttpServletRequest) request).getFile("files"))) {
            files = ((MultipartHttpServletRequest) request).getFiles("files");
        }
        modelClassifyService.update(modelClassify, files);
        return success("success");
    }

    @RequestMapping(value = "/findById/{id}", method = RequestMethod.GET)
    public Object findById(@PathVariable String id) {
        return success(modelClassifyService.findById(id));
    }

    @RequestMapping(value = "/delete/{id}/{type}", method = RequestMethod.GET)
    public Object delete(@PathVariable String id, @PathVariable int type) throws BusinessException {
        modelClassifyService.delete(id, type);
        return success("success");
    }

}
